﻿
@{
    ViewBag.Title = "图片下载";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@section Contents{
    <style>
        .layui-form-label {
            width: 130px;
        }

        .layui-input-inline {
            width: 230px !important;
        }

        .layui-form-item .layui-form-checkbox[lay-skin=primary] {
            margin-top: -5px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <form class="layui-form layui-fluid" action="/Report/PostDownloadImageZip" method="post">
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.BarCode">按组件下载：</label>
                        <div class="layui-input-inline">
                            <textarea class="layui-textarea code"></textarea>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="checkbox" lay-skin="primary" sitetype="1" title="EL1图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="2" title="接线盒图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="3" title="Iv数据" />
                            <input type="checkbox" lay-skin="primary" sitetype="4" title="Iv图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="5" title="EL2图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="6" title="绝缘耐压数据" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-list btndownload" bytype="0" lay-submit lay-filter="btndownload">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.PackNo">按包装下载：</label>
                        <div class="layui-input-inline">
                            <input type="text" value="" class="layui-input code" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="checkbox" lay-skin="primary" sitetype="1" title="EL1图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="2" title="接线盒图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="3" title="Iv数据" />
                            <input type="checkbox" lay-skin="primary" sitetype="4" title="Iv图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="5" title="EL2图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="6" title="绝缘耐压数据" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-list btndownload" bytype="1" lay-submit lay-filter="btndownload">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CabinetNo">按柜下载：</label>
                        <div class="layui-input-inline">
                            <input type="text" value="" class="layui-input code" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="checkbox" lay-skin="primary" sitetype="1" title="EL1图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="2" title="接线盒图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="3" title="Iv数据" />
                            <input type="checkbox" lay-skin="primary" sitetype="4" title="Iv图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="5" title="EL2图片" />
                            <input type="checkbox" lay-skin="primary" sitetype="6" title="绝缘耐压数据" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layuiadmin-btn-list btndownload" bytype="2" lay-submit lay-filter="btndownload">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <input type="hidden" name="byType" value="" />
        <input type="hidden" name="code" value="" />
        <input type="hidden" name="siteTypes" value="" />
    </form>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['layer', 'form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                form.on('submit(btndownload)', function (data) {
                    console.log(data);
                    var $this = $(data.elem);
                    var byType = parseInt($this.attr("bytype"));
                    var code = $($this.parent().prev().prev()).find(".code")[0].value;
                    if (code == "") {
                        return false;
                    }
                    var codes = new Array();
                    if (byType == 0) {
                        codes = code.split('\n');
                    } else {
                        codes.push(code);
                    }
                    var downloadSites = new Array();
                    $($this.parent().prev()).find("input[type=checkbox]").each(function () {
                        if (this.checked) {
                            downloadSites.push(parseInt($(this).attr("sitetype")));
                        }
                    });
                    if (downloadSites.length <= 0) {
                        return false;
                    }

                    downImages(byType, codes, downloadSites);
                    return false;
                });

                function downImages(byType, codes, siteTypes) {
                    if (!window.parent.getConnectionStatus()) {
                        layer.msg("本地工具已断开，请打开本地“MES本地工具”，如未安装，请点击屏幕右上角“下载本地工具”", { icon: 5 });
                        return;
                    }
                    window.parent.layui.layer.open({
                        type: 1,
                        content: '<div style="padding: 50px;">后台正在下载图片，下载完成后将自动打开，请稍后...</div>',
                    });
                    window.parent.parent.sendMessage(JSON.stringify({
                        Command: "DownloadImages",
                        Body: {
                            ByType: byType,
                            Codes: codes,
                            SiteTypes: siteTypes
                        }
                    }));
                }
            });
        }
    </script>
}
